package ProyectoX.Score;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ManipuladorArchivos {
	
	protected BufferedWriter writer;
	protected String fileName;
		
	public ManipuladorArchivos(String fileName){
		this.fileName = fileName;
		try {
			writer = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + File.separator + fileName , true));
		}catch (IOException e) {e.printStackTrace();}
	}

	/**
	* Escribe un String en un archivo txt
	* @param text String a escribir
	*/
	public void writeToFile(String text){
		try {
			writer = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + File.separator + fileName, false));
			writer.write(text);
			writer.close();
		} catch (IOException e) {e.printStackTrace();}
	}
		
	/**
	* Lee desde un archivo txt
	* @return String con lineas leidas y saltos de linea
	*/
	//http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/
	public String readFromFile(){
		String output = "";
		try (BufferedReader br = new BufferedReader(new FileReader(System.getProperty("user.dir") + File.separator + fileName)))
		{
			String sCurrentLine;
	 
			while ((sCurrentLine = br.readLine()) != null) {
				output += sCurrentLine;
				output += System.lineSeparator();
			}
				
		} catch (IOException e) {e.printStackTrace();} 
			
		return output;
	}
}
